package com.example.notificationdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.TaskStackBuilder;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
   private  static final String NOTIFICATION="test";
    private static final int REQUEST_NOTIFI=0xf1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView(){
        Button button=findViewById(R.id.butn_notifi);
        Button button1=findViewById(R.id.butn_now_notifi);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendNotification();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendOriginalNotify();
            }
        });
    }
    private void sendOriginalNotify(){
        NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification=new Notification.Builder(this)
                  .setContentTitle("传统方式通知标题")
                  .setContentText("传统方式通知内容")
                  .setSmallIcon(R.mipmap.ic_launcher).build();
          nm.notify(1,notification);
    }

    private void sendNotification(){
        NotificationManager nm= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //兼容性库创建NotificationManager
        NotificationManagerCompat nmc=NotificationManagerCompat.from(this);
        //构建信道
        //参数1  信道id 全局唯一    参数2  信道名称  在设置中可见  参数3  通知的重要级别
        NotificationChannel channel=new NotificationChannel(NOTIFICATION,"测试信道",
                NotificationManager.IMPORTANCE_HIGH);

        //将信道注册到通知服务中
        nm.createNotificationChannel(channel);
        Intent intent=new Intent(this,NotificationActivity.class);
        //参数1  Context  参数2  requestCode  参数3  跳转到目标Activity的intent   参数4 点击后对通知的后续操作
        //FLAG_UPDATE_CURRENT 通知不销毁  实时更新通知内容 如下载
        //FLAG_CANCEL_CURRENT 点击通知跳转后销毁通知
        //PendingIntent pi=PendingIntent.getActivity(this,REQUEST_NOTIFI,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        TaskStackBuilder task=TaskStackBuilder.create(this);
        //添加Intent  目标Activity必须在AndroidManifest.xml中的<activity>中
        //设置android:parentActivityName属性
        task.addNextIntentWithParentStack(intent);
        PendingIntent pi=task.getPendingIntent(REQUEST_NOTIFI,PendingIntent.FLAG_CANCEL_CURRENT);
        //构造通知体对象
        Notification notification=
                //new NotificationCompat.Builder(this,NOTIFICATION);
                new Notification.Builder(this,NOTIFICATION)
                .setContentTitle("信道标题")
                .setContentText("信道内容")
                //时间设置
                .setWhen(System.currentTimeMillis())
                .setShowWhen(true)
                //设置大图标
                .setLargeIcon(Icon.createWithResource(this,R.mipmap.ic_launcher))
                //设置点击跳转后是否自动销毁通知
                .setAutoCancel(true)
                //设置是否常驻通知栏（不可销毁）
                .setOngoing(false)
                //设置大图
                //.setStyle(new Notification.BigPictureStyle()
                       // .bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.wh300)))
               // 设置大段文字
                .setStyle(new Notification.BigTextStyle().bigText("hhhhhhhhhhhhhhhhhhDescribe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of writeToParcel(android.os.Parcel, int), the return value of this method must include the CONTENTS_FILE_DESCRIPTOR bit."))
                //添加点击后续操作
                .setContentIntent(pi)
                .setSmallIcon(R.mipmap.ic_launcher).build();
        //参数1 id的值在每次发送通知时均相同，则通知会覆盖
        //若每次消息的id值不同，则对应通知栏创建新的通知对象
        nm.notify(1,notification);
       //兼容性库发送通知  nmc.notify(1,notification);

    }
}